package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import utility.MD5;
import utility.StringUtil;
import clientMain.Login;
import clientUtilGUI.UtilityGUI;
import database.UserDelegate;

public class User_ChangePass extends JDialog implements ActionListener{
	
	private JPasswordField fPwdNew;
	private JPasswordField fPwdOld;
	private JPasswordField fPwdConfirm;
	private GridBagConstraints gbc_panelBtn;
	private JPanel panelBtn;
	private JButton btnSave;
	private JButton btnCancel;
	private JLabel lblError;
	
	public User_ChangePass(JFrame main) {
		super(main, "Đổi Mật Khẩu", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 180;
	    int width = 310;
	    setSize(width, height);
	    setLocation(UtilityGUI.screenSize.width - width - 5, 140);
	    
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.5};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblPwdOld = new JLabel("Mật khẩu hiện tại :");
		lblPwdOld.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblPwdOld = new GridBagConstraints();
		gbc_lblPwdOld.anchor = GridBagConstraints.EAST;
		gbc_lblPwdOld.insets = new Insets(10, 10, 5, 5);
		gbc_lblPwdOld.gridx = 0;
		gbc_lblPwdOld.gridy = 0;
		getContentPane().add(lblPwdOld, gbc_lblPwdOld);
		
		fPwdOld = new JPasswordField();
		fPwdOld.setColumns(20);
		GridBagConstraints gbc_pwdOld = new GridBagConstraints();
		gbc_pwdOld.fill = GridBagConstraints.BOTH;
		gbc_pwdOld.insets = new Insets(10, 0, 5, 10);
		gbc_pwdOld.gridx = 1;
		gbc_pwdOld.gridy = 0;
		getContentPane().add(fPwdOld, gbc_pwdOld);
		
		JLabel lblPwdNew = new JLabel("Mật khẩu mới :");
		lblPwdNew.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblPwdNew = new GridBagConstraints();
		gbc_lblPwdNew.anchor = GridBagConstraints.EAST;
		gbc_lblPwdNew.insets = new Insets(2, 10, 5, 5);
		gbc_lblPwdNew.gridx = 0;
		gbc_lblPwdNew.gridy = 1;
		getContentPane().add(lblPwdNew, gbc_lblPwdNew);
		
		fPwdNew = new JPasswordField();
		GridBagConstraints gbc_pwdNew = new GridBagConstraints();
		gbc_pwdNew.insets = new Insets(2, 0, 5, 10);
		gbc_pwdNew.fill = GridBagConstraints.BOTH;
		gbc_pwdNew.gridx = 1;
		gbc_pwdNew.gridy = 1;
		getContentPane().add(fPwdNew, gbc_pwdNew);
		
		JLabel lblPwdConfirm = new JLabel("Xác nhận mật khẩu :");
		lblPwdConfirm.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblPwdConfirm = new GridBagConstraints();
		gbc_lblPwdConfirm.anchor = GridBagConstraints.EAST;
		gbc_lblPwdConfirm.insets = new Insets(2, 10, 5, 5);
		gbc_lblPwdConfirm.gridx = 0;
		gbc_lblPwdConfirm.gridy = 2;
		getContentPane().add(lblPwdConfirm, gbc_lblPwdConfirm);
		
		fPwdConfirm = new JPasswordField();
		GridBagConstraints gbc_pwdConfirm = new GridBagConstraints();
		gbc_pwdConfirm.insets = new Insets(2, 0, 5, 10);
		gbc_pwdConfirm.fill = GridBagConstraints.BOTH;
		gbc_pwdConfirm.gridx = 1;
		gbc_pwdConfirm.gridy = 2;
		getContentPane().add(fPwdConfirm, gbc_pwdConfirm);
		
		lblError = new JLabel("");
		lblError.setFont(UtilityGUI.fontNote);
		lblError.setForeground(UtilityGUI.colorNote);
		GridBagConstraints gbc_lblError = new GridBagConstraints();
		gbc_lblError.anchor = GridBagConstraints.WEST;
		gbc_lblError.gridwidth = 2;
		gbc_lblError.insets = new Insets(2, 10, 5, 10);
		gbc_lblError.gridx = 0;
		gbc_lblError.gridy = 3;
		getContentPane().add(lblError, gbc_lblError);
		
		panelBtn = new JPanel();
		gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.gridwidth = 2;
		gbc_panelBtn.insets = new Insets(2, 10, 10, 10);
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.gridx = 0;
		gbc_panelBtn.gridy = 4;
		getContentPane().add(panelBtn, gbc_panelBtn);
		
		btnSave = new JButton(UtilityGUI.btnSave);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		btnSave.addActionListener(this);
		panelBtn.add(btnSave);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		panelBtn.add(btnCancel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			lblError.setText("");
			String pwdOld = fPwdOld.getText();
			String pwdNew = fPwdNew.getText();
			String pwdConfirm = fPwdConfirm.getText();
			pwdOld = MD5.MD5Password(pwdOld);
			if(!pwdOld.equals(Login.listAuthorization.get(0).getUserPwd())){
				lblError.setText("Mật khẩu cũ không chính xác!");
				return;
			}
			if(!StringUtil.isValidPwd(pwdNew)){
				lblError.setText("Mật khẩu độ dài lớn hơn 6 và không có kí tự Việt!");
				return;
			}
			if(!pwdConfirm.equals(pwdNew)){
				lblError.setText("Xác nhận mật khẩu phải giống mật khẩu!");
				return;
			}
			pwdNew = MD5.MD5Password(pwdNew);
			String userName = Login.listAuthorization.get(0).getUserName();
			UserDelegate action = new UserDelegate();
			if(action.changePwd(userName, pwdNew)){
				dispose();
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
						UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
			}else{
				lblError.setText(UtilityGUI.errorAction);
				return;
			}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
	}

}
